require(['../config'], () => {
    require(['request', 'template', 'header','bootstrap','footer'], (request, template) => {
      class Index{
        constructor () {
            this.getIndexListContent()
            this.cart=JSON.parse(localStorage.getItem('cart')||'[]')
        }
        banner () {
          // 实现轮播图功能
          var mySwiper = new Swiper ('.swiper-container', {
            loop: true, // 循环模式选项
            effect : 'fade',
            autoplay:{
                delay: 1500,
                disableOnInteraction: false
            },
            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination',
              clickable :true
            },
            
            // 如果需要滚动条
            scrollbar: {
              el: '.swiper-scrollbar',
            },
          })    
          //鼠标覆盖停止自动切换
            mySwiper.el.onmouseover = function(){
            mySwiper.autoplay.stop();
          }
          
          //鼠标离开开始自动切换
            mySwiper.el.onmouseout = function(){
            mySwiper.autoplay.start();
          }  
        }
        getIndexListContent(){
          request.getIndexList().then(value=>{
            const bannerData=value.data.banners
            const {topList,items}=value.data
            const linkList= items.list.filter(val=>{
                              return val.type===2
                            }).slice(0,4)
            const contentList=items.list.slice(0,24)
            $('#banner_warp').append( template("banner",{bannerList:bannerData}))
            $('#index_content_top_warp').append( template("index_content_top",{topList}))
            $('#index_link_warp').append( template("index_link",{linkList}))
            $('#index_content_center_warp').append( template("index_content_center",{contentList}))
            this.banner()
            this.moveTopData()
          })
        } 

        moveTopData(){
          const $goprev=$('#top_goprev')
          const $gonext=$('#top_gonext')
          const $warp= $('#index_content_top_warp')
          const liSize=$warp.children('li').length
          const outerWidth=$warp.children('li').outerWidth()
          let start=0
          $warp.css({width:outerWidth*liSize})
          $gonext.on('click',function(){
            start++
            if(start===1){
              $goprev.removeAttr('disabled').removeClass('disabled')
            }
            if(start===liSize-4){
              $gonext.attr('disabled','disabled')
              $gonext.addClass('disabled')
            }
            if(start>liSize-4){
               return
            }
            $warp.animate({left:-outerWidth*start},500,()=>{console.log(start)})
          })

          $goprev.on('click',function(){
            start--
            if(start===liSize-5){
              $gonext.removeAttr('disabled','disabled').removeClass('disabled')
            }
            if(start===0){
              $goprev.attr('disabled','disabled').addClass('disabled')
            }
            if(start<0){
              return
            }
            $warp.animate({left:-outerWidth*start},500,()=>{console.log(start)})
          })
        }

        toggleData(){

        }
      }
      new Index()
    })
  })
       
  function searchValue(data){
    const $searchTitle=$('#title')
    data=data.s
			let fragment=document.createDocumentFragment()
			data.forEach(val=>{
				let li=document.createElement("li")
				li.innerText=val
				fragment.appendChild(li)
				$searchTitle.html("")
			})
			$searchTitle.append(fragment)
  }